<template>
  <chat-renderer ref="renderer"
  :maxNumber="config.maxNumber"
  :showGiftInfo="config.showGiftInfo"
  :minGiftPrice="config.minGiftPrice"
  :minTickerPrice="config.minTickerPrice"
  :danmakuAtBottom="config.danmakuAtBottom"
  :tickerAtButtom="config.tickerAtButtom"
  ></chat-renderer>
</template>

<script>
import * as i18n from '@/i18n'
import { mergeConfig, toBool, toInt, toFloat, getUuid4Hex } from '@/utils'
import * as trie from '@/utils/trie'
import * as pronunciation from '@/utils/pronunciation'
import * as chatConfig from '@/api/chatConfig'
import * as chat from '@/api/chat'
import * as chatModels from '@/api/chat/models'
import ChatRenderer from '@/components/ChatRenderer'
import * as constants from '@/components/ChatRenderer/constants'
import ChatClientTest from '@/api/chat/ChatClientTest'
import ChatClientDirectWeb from '@/api/chat/ChatClientDirectWeb'
import ChatClientDirectOpenLive from '@/api/chat/ChatClientDirectOpenLive'
import ChatClientRelay from '@/api/chat/ChatClientRelay'


export default {
  name: 'Room',
  components: {
    ChatRenderer
  },
  props: {
    roomKeyType: {
      type: Number,
      default: 1
    },
    roomKeyValue: {
      type: [Number, String],
      default: null
    },
    strConfig: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    let customStyleElement = document.createElement('style')
    document.head.appendChild(customStyleElement)
    return {
      config: chatConfig.getLocalConfig(),

      chatClient: null,
      textEmoticons: [], // 官方的文本表情（后端配置的）
      pronunciationConverter: null,

      customStyleElement, // 仅用于样式生成器中预览样式
      presetCssLinkElement: null,
      skinConfigList: [
        {
          name: '空样式',
          img: "https://2023blivechat.oss-cn-hangzhou.aliyuncs.com/skin/%E8%8A%B1%E8%8A%B1%281%29.png",
          url: 'https://blivechat.oss-cn-beijing.aliyuncs.com/DM/css/mystyle1.css',
          value: 'normal'
        },
        {
          name: '碎冰蓝',
          img: "https://2023blivechat.oss-cn-hangzhou.aliyuncs.com/skin/%E5%87%BA%E6%B8%B8%E5%9B%BE%E6%A0%87.png",
          url: 'https://blivechat.oss-cn-beijing.aliyuncs.com/mycat/css/A38%E7%A2%8E%E5%86%B0%E8%93%9D.%E8%BF%B7%E5%A2%83%E5%8F%8C%E7%94%9F.css',
          value: '001'
        }
      ],
    }
  },
  computed: {
    blockKeywordsTrie() {
      let blockKeywords = this.config.blockKeywords.split('\n')
      let res = new trie.Trie()
      for (let keyword of blockKeywords) {
        if (keyword !== '') {
          res.set(keyword, true)
        }
      }
      return res
    },
    blockUsersSet() {
      let blockUsers = this.config.blockUsers.split('\n')
      blockUsers = blockUsers.filter(user => user !== '')
      return new Set(blockUsers)
    },
    emoticonsTrie() {
      let res = new trie.Trie()
      for (let emoticons of [this.config.emoticons, this.textEmoticons]) {
        for (let emoticon of emoticons) {
          if (emoticon.keyword !== '' && emoticon.url !== '') {
            res.set(emoticon.keyword, emoticon)
          }
        }
      }
      return res
    }
  },
  mounted() {
    if (document.visibilityState === 'visible') {
      if (this.roomKeyValue === null) {
        this.init()
      } else {
        // 正式房间要随机延迟加载，防止同时请求导致雪崩
        window.setTimeout(this.init, Math.random() * 3000)
      }
    } else {
      // 当前窗口不可见，延迟到可见时加载，防止OBS中一次并发太多请求（OBS中浏览器不可见时也会加载网页，除非显式设置）
      document.addEventListener('visibilitychange', this.onVisibilityChange)
    }

    window.addEventListener('message', this.onWindowMessage)
    this.getSkin()
  },
  beforeDestroy() {
    window.removeEventListener('message', this.onWindowMessage)

    document.removeEventListener('visibilitychange', this.onVisibilityChange)
    if (this.chatClient) {
      this.chatClient.stop()
    }

    document.head.removeChild(this.customStyleElement)
    if (this.presetCssLinkElement) {
      document.head.removeChild(this.presetCssLinkElement)
    }
  },
  methods: {
    onVisibilityChange() {
      if (document.visibilityState !== 'visible') {
        return
      }
      document.removeEventListener('visibilitychange', this.onVisibilityChange)
      this.init()
    },
    // 获取皮肤
    getSkin() {
      console.log(this.config)
      let skinConfig = this.skinConfigList.find(item => item.value == this.config.skinConfig)
      this.config.customCss = skinConfig.url
      this.setCustomCss()
    },
    setCustomCss() {
      console.log("custom css is")
      console.log(this.config.customCss)
      let e = document.querySelector("#custom-css")
      if (e) {
        e.href = this.config.customCss
        // "" === this.config.customCss && e.remove()
      } else {
        let link = document.createElement("link")
        link.id = "custom-css"
        link.rel = "stylesheet"
        link.href = this.config.customCss
        document.head.appendChild(link)
      }
    },
    init() {
      this.initConfig()
      this.initChatClient()
      this.initTextEmoticons()
      if (this.config.giftUsernamePronunciation !== '') {
        this.pronunciationConverter = new pronunciation.PronunciationConverter()
        this.pronunciationConverter.loadDict(this.config.giftUsernamePronunciation)
      }

      // 提示用户已加载
      this.$message({
        message: '加载成功~',
        type: 'success',
        duration: 500
      })
    },
    initConfig() {
      let locale = this.strConfig.lang
      if (locale) {
        i18n.setLocale(locale)
      }

      let cfg = {}
      // 留空的使用默认值
      for (let i in this.strConfig) {
        if (this.strConfig[i] !== '') {
          cfg[i] = this.strConfig[i]
        }
      }
      cfg = mergeConfig(cfg, chatConfig.deepCloneDefaultConfig())

      cfg.showDanmaku = toBool(cfg.showDanmaku)
      cfg.showGift = toBool(cfg.showGift)
      cfg.showGiftName = toBool(cfg.showGiftName)
      cfg.mergeSimilarDanmaku = toBool(cfg.mergeSimilarDanmaku)
      cfg.mergeGift = toBool(cfg.mergeGift)
      cfg.maxNumber = toInt(cfg.maxNumber, chatConfig.DEFAULT_CONFIG.maxNumber)

      cfg.showInteractWordEnter = toBool(cfg.showInteractWordEnter)
      cfg.showInteractWordFollow = toBool(cfg.showInteractWordFollow)
      cfg.showInteractWordShare = toBool(cfg.showInteractWordShare)

      cfg.showSuperchat = toBool(cfg.showSuperchat)
      cfg.showNewMember = toBool(cfg.showNewMember)
      cfg.showGift = toBool(cfg.showGift)
      cfg.showGiftInfo = toBool(cfg.showGiftInfo)

      cfg.minGiftPrice = toFloat(cfg.minGiftPrice, chatConfig.DEFAULT_CONFIG.minGiftPrice)
      cfg.minTickerPrice = toFloat(cfg.minTickerPrice, chatConfig.DEFAULT_CONFIG.minTickerPrice)

      cfg.danmakuAtBottom = toBool(cfg.danmakuAtBottom)
      cfg.tickerAtButtom = toBool(cfg.tickerAtButtom)
      
      cfg.blockGiftDanmaku = toBool(cfg.blockGiftDanmaku)
      cfg.blockLevel = toInt(cfg.blockLevel, chatConfig.DEFAULT_CONFIG.blockLevel)
      cfg.blockNewbie = toBool(cfg.blockNewbie)
      cfg.blockNotMobileVerified = toBool(cfg.blockNotMobileVerified)
      cfg.blockMedalLevel = toInt(cfg.blockMedalLevel, chatConfig.DEFAULT_CONFIG.blockMedalLevel)

      cfg.relayMessagesByServer = toBool(cfg.relayMessagesByServer)
      cfg.autoTranslate = toBool(cfg.autoTranslate)
      cfg.importPresetCss = toBool(cfg.importPresetCss)

      cfg.emoticons = this.toObjIfJson(cfg.emoticons)

      chatConfig.sanitizeConfig(cfg)
      this.config = cfg
    },
    toObjIfJson(str) {
      if (typeof str !== 'string') {
        return str
      }
      try {
        return JSON.parse(str)
      } catch {
        return {}
      }
    },
    initChatClient() {
      if (this.roomKeyValue === null) {
        this.chatClient = new ChatClientTest(this.config.minDanmakuInterval, this.config.maxDanmakuInterval)
      } else if (this.config.relayMessagesByServer) {
        let roomKey = {
          type: this.roomKeyType,
          value: this.roomKeyValue
        }
        this.chatClient = new ChatClientRelay(roomKey, this.config.autoTranslate)
      } else {
        if (this.roomKeyType === 1) {
          this.chatClient = new ChatClientDirectWeb(this.roomKeyValue)
        } else {
          this.chatClient = new ChatClientDirectOpenLive(this.roomKeyValue)
        }
      }
      this.chatClient.onAddText = this.onAddText
      this.chatClient.onAddGift = this.onAddGift
      this.chatClient.onAddMember = this.onAddMember
      this.chatClient.onAddSuperChat = this.onAddSuperChat
      this.chatClient.onDelSuperChat = this.onDelSuperChat
      this.chatClient.onUpdateTranslation = this.onUpdateTranslation
      // this.chatClient.onInteractWord = this.onInteractWord
      this.chatClient.onFatalError = this.onFatalError
      this.chatClient.start()
    },
    async initTextEmoticons() {
      this.textEmoticons = await chat.getTextEmoticons()
    },

    start() {
      this.chatClient.start()
    },
    stop() {
      this.chatClient.stop()
    },

    // 处理样式生成器发送的消息
    onWindowMessage(event) {
      if (event.origin !== window.location.origin) {
        console.warn(`消息origin错误，${event.origin} != ${window.location.origin}`)
        return
      }

      let { type, data } = event.data
      switch (type) {
      case 'roomSetCustomStyle':
        this.customStyleElement.innerText = data.css
        break
      case 'roomStartClient':
        if (this.chatClient) {
          this.chatClient.start()
        }
        break
      case 'roomStopClient':
        if (this.chatClient) {
          this.chatClient.stop()
        }
        break
      }
    },
    
    onInteractWord(data) {
      // console.log(`${data.authorName} 进入房间，data 是 ${JSON.stringify(data, null, 4)}`)

      // NOTE: 判断不同的 Interact 是否显示（进入房间、关注房间、分享房间）
      if (data.msgType === constants.INTERACT_TYPE_ENTER) {
        if (!this.config.showInteractWordEnter) {
          return
        }
      } else if (data.msgType === constants.INTERACT_TYPE_FOLLOW || data.msgType === constants.INTERACT_TYPE_SPECIAL_FOLLOW || data.msgType === constants.INTERACT_TYPE_MUTUAL_FOLLOW) {
        if (!this.config.showInteractWordFollow) {
          return
        }
      } else if (data.msgType === constants.INTERACT_TYPE_SHARE) {
        if (!this.config.showInteractWordShare) {
          return
        }
      } else {
        // 不满足指定类型的互动
        return
      }

      if (!this.filterInteractMessage(data)) {
        return
      }


      let message = {
        id: data.id,
        type: constants.MESSAGE_TYPE_INTERACT,
        avatarUrl: data.avatarUrl,
        time: new Date(data.timestamp * 1000),
        msgType: data.msgType,
        authorName: data.authorName,
        authorNamePronunciation: this.getPronunciation(data.authorName),

        medalName: data.medalName,
        medalLevel: data.medalLevel,
        isFanGroup: data.isFanGroup,

        privilegeType: data.privilegeType,

      }

      this.$refs.renderer.addMessage(message)
    },

    async onAddText(data) {
      if (!this.config.showDanmaku || !this.filterTextMessage(data)) {
        return
      }
      let richContent = await this.getRichContent(data)
      // 合并要放在异步调用后面，因为异步调用后可能有新的消息，会漏合并
      // if (this.mergeSimilarText(data.content)) {
      //   return
      // }
      let message = {
        id: data.id,
        type: constants.MESSAGE_TYPE_TEXT,
        avatarUrl: data.avatarUrl,
        time: new Date(data.timestamp * 1000),
        authorName: data.authorName,
        authorType: data.authorType,
        content: data.content,
        richContent: richContent,
        privilegeType: data.privilegeType,
        repeated: 1,
        translation: data.translation
      }
      this.$refs.renderer.addMessage(message)
    },
    // /** @param {chatModels.AddGiftMsg} data */
    // onAddGift(data) {
    //   if (!this.config.showGift) {
    //     return
    //   }
    //   let price = data.totalCoin / 1000
    //   if (this.mergeSimilarGift(data.authorName, price, data.totalFreeCoin, data.giftName, data.num)) {
    //     return
    //   }
    //   if (price < this.config.minGiftPrice) { // 丢人
    //     return
    //   }
    //   let message = {
    //     id: data.id,
    //     type: constants.MESSAGE_TYPE_GIFT,
    //     avatarUrl: data.avatarUrl,
    //     time: new Date(data.timestamp * 1000),
    //     authorName: data.authorName,
    //     authorNamePronunciation: this.getPronunciation(data.authorName),
    //     price: price,
    //     // freePrice: data.totalFreeCoin, // 暂时没用到
    //     giftName: data.giftName,
    //     num: data.num
    //   }
    //   this.$refs.renderer.addMessage(message)
    // },

    onAddGift(data) {
      console.log('gift', data)
      if (!this.config.showGift) {
        // console.log("收到礼物，但是否显示礼物为" + this.config.showGift)
        return
      }
      if (this.config.showTranslateDanmakuOnly) {
        // console.log("只显示以“"+ this.config.translationSign +"”开头的翻译弹幕")
        return
      }
      // let paid = data.coin_type == 'gold' ? true : false

      // let price = paid ? data.price * data.num / 1000 : 0

      let price = data.total_coin / 1000
      if (this.mergeSimilarGift(data.uname, price, data.giftName, data.num)) {
        return
      }

      let message = {
        id: getUuid4Hex(),
        type: constants.MESSAGE_TYPE_GIFT,
        avatarUrl: data.face,
        time: new Date(data.timestamp * 1000),
        authorName: data.uname,
        authorNamePronunciation: this.getPronunciation(data.uname),
        price: price,
        giftName: data.giftName,
        num: data.num,
      }
      this.$refs.renderer.addMessage(message)
    },
    onAddMember(data) {
      if (!this.config.showGift || !this.filterNewMemberMessage(data)) {
        return
      }
      let message = {
        id: data.id,
        type: constants.MESSAGE_TYPE_MEMBER,
        avatarUrl: data.avatarUrl,
        time: new Date(data.timestamp * 1000),
        authorName: data.authorName,
        authorNamePronunciation: this.getPronunciation(data.authorName),
        privilegeType: data.privilegeType,
        title: this.$t('chat.membershipTitle')
      }
      this.$refs.renderer.addMessage(message)
    },

    onAddSuperChat(data) {
      if (!this.config.showSuperchat || !this.filterSuperChatMessage(data)) {
        return
      }
      if (data.price < this.config.minGiftPrice) {
        return
      }
      let message = {
        id: data.id,
        type: constants.MESSAGE_TYPE_SUPER_CHAT,
        avatarUrl: data.avatarUrl,
        authorName: data.authorName,
        authorNamePronunciation: this.getPronunciation(data.authorName),
        price: data.price,
        time: new Date(data.timestamp * 1000),
        content: data.content.trim(),
        translation: data.translation
      }
      this.$refs.renderer.addMessage(message)
    },

    onDelSuperChat(data) {
      this.$refs.renderer.delMessages(data.ids)
    },

    onUpdateTranslation(data) {
      if (!this.config.autoTranslate) {
        return
      }
      this.$refs.renderer.updateMessage(data.id, { translation: data.translation })
    },

    onFatalError(error) {
      this.$message.error({
        message: error.toString(),
        duration: 10 * 1000
      })
      this.onAddText(new chatModels.AddTextMsg({
        authorName: 'blivechat',
        authorType: constants.AUTHOR_TYPE_ADMIN,
        content: this.$t('room.fatalErrorOccurred'),
        authorLevel: 60,
      }))

      if (error.type === chatModels.FATAL_ERROR_TYPE_AUTH_CODE_ERROR) {
        // Read The Fucking Manual
        this.$router.push({ name: 'help' })
      }
    },

    filterTextMessage(data) {
      if (this.config.blockGiftDanmaku && data.isGiftDanmaku) {
        return false
      } else if (this.config.blockLevel > 0 && data.authorLevel < this.config.blockLevel) {
        return false
      } else if (this.config.blockNewbie && data.isNewbie) {
        return false
      } else if (this.config.blockNotMobileVerified && !data.isMobileVerified) {
        return false
      } else if (this.config.blockMedalLevel > 0 && data.medalLevel < this.config.blockMedalLevel) {
        return false
      }
      return this.filterByContent(data.content) && this.filterByAuthorName(data.authorName)
    },
    filterSuperChatMessage(data) {
      return this.filterByContent(data.content) && this.filterByAuthorName(data.authorName)
    },
    filterNewMemberMessage(data) {
      return this.filterByAuthorName(data.authorName)
    },
    filterByContent(content) {
      let blockKeywordsTrie = this.blockKeywordsTrie
      for (let i = 0; i < content.length; i++) {
        let remainContent = content.substring(i)
        if (blockKeywordsTrie.lazyMatch(remainContent) !== null) {
          return false
        }
      }
      return true
    },
    filterByAuthorName(authorName) {
      return !this.blockUsersSet.has(authorName)
    },
    mergeSimilarText(content) {
      if (!this.config.mergeSimilarDanmaku) {
        return false
      }
      return this.$refs.renderer.mergeSimilarText(content)
    },
    mergeSimilarGift(authorName, price, giftName, num) {
      console.log('mergeSimilarGift', this.config.mergeGift)
      if (!this.config.mergeGift) {
        return false
      }
      return this.$refs.renderer.mergeSimilarGift(authorName, price, giftName, num)
    },
    getPronunciation(text) {
      if (this.pronunciationConverter === null) {
        return ''
      }
      return this.pronunciationConverter.getPronunciation(text)
    },
    async getRichContent(data) {
      let richContent = []

      // 官方的非文本表情
      if (data.emoticon !== null) {
        richContent.push({
          type: constants.CONTENT_TYPE_IMAGE,
          text: data.content,
          url: data.emoticon,
          width: 0,
          height: 0
        })
        await this.fillImageContentSizes(richContent)
        return richContent
      }

      // 没有文本表情，只能是纯文本
      if (this.config.emoticons.length === 0 && this.textEmoticons.length === 0) {
        richContent.push({
          type: constants.CONTENT_TYPE_TEXT,
          text: data.content
        })
        return richContent
      }

      // 可能含有文本表情，需要解析
      let emoticonsTrie = this.emoticonsTrie
      let startPos = 0
      let pos = 0
      while (pos < data.content.length) {
        let remainContent = data.content.substring(pos)
        let matchEmoticon = emoticonsTrie.lazyMatch(remainContent)
        if (matchEmoticon === null) {
          pos++
          continue
        }

        // 加入之前的文本
        if (pos !== startPos) {
          richContent.push({
            type: constants.CONTENT_TYPE_TEXT,
            text: data.content.slice(startPos, pos)
          })
        }

        // 加入表情
        richContent.push({
          type: constants.CONTENT_TYPE_IMAGE,
          text: matchEmoticon.keyword,
          url: matchEmoticon.url,
          width: 0,
          height: 0
        })
        pos += matchEmoticon.keyword.length
        startPos = pos
      }
      // 加入尾部的文本
      if (pos !== startPos) {
        richContent.push({
          type: constants.CONTENT_TYPE_TEXT,
          text: data.content.slice(startPos, pos)
        })
      }

      await this.fillImageContentSizes(richContent)
      return richContent
    },
    async fillImageContentSizes(richContent) {
      let urlSizeMap = new Map()
      for (let content of richContent) {
        if (content.type === constants.CONTENT_TYPE_IMAGE) {
          urlSizeMap.set(content.url, { width: 0, height: 0 })
        }
      }
      if (urlSizeMap.size === 0) {
        return
      }

      let promises = []
      for (let url of urlSizeMap.keys()) {
        let urlInClosure = url
        promises.push(new Promise(
          resolve => {
            let img = document.createElement('img')
            img.onload = () => {
              let size = urlSizeMap.get(urlInClosure)
              size.width = img.naturalWidth
              size.height = img.naturalHeight
              resolve()
            }
            // 获取失败了默认为0
            img.onerror = resolve
            // 超时保底
            window.setTimeout(resolve, 5000)
            img.src = urlInClosure
          }
        ))
      }
      await Promise.all(promises)

      for (let content of richContent) {
        if (content.type === constants.CONTENT_TYPE_IMAGE) {
          let size = urlSizeMap.get(content.url)
          content.width = size.width
          content.height = size.height
        }
      }
    }
  }
}
</script>
